//
//  LoginViewController.m
//  礼物go
//
//  Created by dllo on 16/6/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LoginViewController.h"
#import "UMSocialSnsPlatformManager.h"
#import "RegistViewController.h"
#import "UMSocial.h"


@interface LoginViewController ()

@property(nonatomic,strong)UIButton *cancelButton;
@property(nonatomic,strong)UIImageView *phoneImageView;
@property(nonatomic,strong)UIImageView *passWordImageView;
@property(nonatomic,strong)UIButton *loginButton;
@property(nonatomic,strong)UITextField *phoneTextField;
@property(nonatomic,strong)UITextField *passwordTextField;
@property(nonatomic,strong)UIButton *regiestButton;


@property(nonatomic,strong)UIImageView *SDKView;
@property(nonatomic,strong)UIButton *sinaButton;
@property(nonatomic,strong)UIButton *wechatButton;
@property(nonatomic,strong)UIButton *qqButton;



@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelButton.frame = CGRectMake(10, 40, 15, 15);
    [self.cancelButton setImage:[UIImage imageNamed:@"btn_close_nightmode_14x14_@2x"] forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview:self.cancelButton];
    
    self.regiestButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.regiestButton.frame = CGRectMake(SCREENWIDTH - 60, self.cancelButton.frame.origin.y - 10, 40, 40);
    [self.regiestButton setImage:[UIImage imageNamed:@"btn_increase_normal_24x24_@3x"] forState:UIControlStateNormal];
    //self.regiestButton.imageView.frame = self.regiestButton.frame;
    
    [self.regiestButton addTarget:self action:@selector(registButtonAction) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview:self.regiestButton];

    
    
    self.phoneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 160, 20, 20)];
    self.phoneImageView.image = [UIImage imageNamed:@"icon_signin_cellphone_14x16_@2x"];
    [self.view addSubview:self.phoneImageView];
    
    self.phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.phoneImageView.frame) + 10, self.phoneImageView.frame.origin.y - 5, 260, 28)];
    self.phoneTextField.placeholder = @"请输入账户名";
 self.phoneTextField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [self.view addSubview:self.phoneTextField];
    self.phoneTextField.layer.borderWidth = 0.8;
    
    self.passWordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.phoneImageView.frame.origin.x, CGRectGetMaxY(self.phoneTextField.frame) + 25, self.phoneImageView.frame.size.width, self.phoneImageView.frame.size.height)];
    self.passWordImageView.image = [UIImage imageNamed:@"icon_signin_password_14x16_@2x"];
    [self.view addSubview:self.passWordImageView];
    
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.passWordImageView.frame) + 10, self.passWordImageView.frame.origin.y - 5, self.phoneTextField.frame.size.width, self.phoneTextField.frame.size.height)];
    self.passwordTextField.placeholder = @"请输入密码";
    self.passwordTextField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [self.view addSubview:self.passwordTextField];
    self.passwordTextField.layer.borderWidth = 0.8;
    
    self.loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.loginButton.frame = CGRectMake(self.passWordImageView.frame.origin.x + 5, self.passwordTextField.frame.origin.y + 60, self.passWordImageView.frame.size.width + self.passwordTextField.frame.size.width + 5, 30);
    self.loginButton.backgroundColor = [UIColor redColor];
    [self.loginButton setTitle:@"登录" forState: UIControlStateNormal];
    [self.loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:self.loginButton];
    [self.loginButton addTarget:self action:@selector(loginButtonAction) forControlEvents: UIControlEventTouchUpInside];
    self.loginButton.layer.masksToBounds = YES;
    self.loginButton.layer.cornerRadius = 5;
    
    self.SDKView = [[UIImageView alloc] initWithFrame:CGRectMake(self.loginButton.frame.origin.x, self.view.frame.size.height - 220, self.loginButton.frame.size.width, 30)];
    self.SDKView.image = [UIImage imageNamed:@"sdkView"];
    [self.view addSubview:self.SDKView];
    
    
    self.sinaButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.sinaButton.frame = CGRectMake(self.SDKView.frame.origin.x + 20, CGRectGetMaxY(self.SDKView.frame) + 20, 50, 50);
    [self.sinaButton setImage:[UIImage imageNamed:@"icon_log_sina_63x63_@2x"] forState:UIControlStateNormal];
    [self.sinaButton addTarget:self action:@selector(sinaButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.sinaButton];
    
    self.wechatButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.wechatButton.frame = CGRectMake(CGRectGetMidX(self.sinaButton.frame) + 80, CGRectGetMaxY(self.SDKView.frame) + 20, 50, 50);
    [self.wechatButton setImage:[UIImage imageNamed:@"icon_log_wechat_63x63_@2x"] forState:UIControlStateNormal];
    [self.wechatButton addTarget:self action:@selector(wechatButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.wechatButton];

    self.qqButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.qqButton.frame = CGRectMake(CGRectGetMidX(self.wechatButton.frame) + 80, CGRectGetMaxY(self.SDKView.frame) + 20, 50, 50);
    [self.qqButton setImage:[UIImage imageNamed:@"icon_log_qq_63x63_@2x"] forState:UIControlStateNormal];
    [self.qqButton addTarget:self action:@selector(qqButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.qqButton];
    
    
    // Do any additional setup after loading the view.
}

-(void)qqButtonAction{

    
}

-(void)wechatButtonAction{

    
}


-(void)sinaButtonAction{
    //登录第二步
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {//登录成功
            
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *string = @"yes";
        [user setObject:string forKey:@"status"];
        [user setObject:snsAccount.userName forKey:@"userName"];
        [user setObject:snsAccount.iconURL forKey:@"userIcon"];
            
        [user synchronize];

            
            
           // NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            
            
            //登录第三步 ：在登录成功后 获取用户信息
            
            //获取accestoken以及新浪用户信息，得到的数据在回调Block对象形参respone的data属性
            [[UMSocialDataService defaultDataService] requestSnsInformation:UMShareToSina  completion:^(UMSocialResponseEntity *response){
                //NSLog(@"SnsInformation is %@",response.data);
                [self dismissViewControllerAnimated:YES completion:^{
                    
                    
                }];
            }];
            
            
            
        }});

}

-(void)loginButtonAction{
 NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    //获取密码
    NSString *password = [[DataFMDBHandle shareDataFMDBHandle] selectAccount:self.phoneTextField.text];
    if (password == self.passwordTextField.text) {
        NSString *string = @"yes";
        [user setObject:string forKey:@"status"];
        [user setObject:self.phoneTextField.text forKey:@"userName"];
        [user synchronize];
        [self dismissViewControllerAnimated:YES completion:^{
            
            
        }];
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名或密码错误" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
    }
    
 }

-(void)registButtonAction{
    RegistViewController *registVC = [[RegistViewController alloc] init];
    [registVC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self presentViewController:registVC animated:YES completion:^{
    
    
}];

}


-(void)cancelButtonAction{
[self dismissViewControllerAnimated:YES completion:^{
    
    
}];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
